package com.example.javascaffold.utils;


import com.example.javascaffold.constant.ReturnConstant;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * Controller 层返回结果
 * 在很大程度上兼容了原项目组的BaseController
 * 以之前团队的习惯为准
 * @author song
 * @date 14:51 2021/11/22
 * @param 
 * @return 
 * @other
**/
@Data
public class BaseController {

    /**
     * 成功时调用
     * @author song
     * @date 14:51 2021/11/22
     * @param
     * @return
     * @other
     **/
    public Map<String,Object> render(){
        Map<String,Object> result = new HashMap<>(3);
        result.put(ReturnConstant.STATUS, ReturnConstant.STATUS_SUCCESS);
        result.put(ReturnConstant.MSG, ReturnConstant.MSG_OP_SUCCESS);
        result.put(ReturnConstant.CODE, ReturnConstant.CODE_SUCCESS);
        return result;
    }

    /**
     * 返回成功 携带数据
     * @author song
     * @date 14:52 2021/11/22
     * @param 
     * @return 
     * @other
    **/
    public Map<String,Object> render(Object object){
        Map<String,Object> result = new HashMap<>(4);
        result.put(ReturnConstant.STATUS, ReturnConstant.STATUS_SUCCESS);
        result.put(ReturnConstant.MSG, ReturnConstant.MSG_OP_SUCCESS);
        result.put(ReturnConstant.CODE, ReturnConstant.CODE_SUCCESS);
        result.put(ReturnConstant.DATA, object);
        return result;
    }

    /**
     * 请求失败
     * @author song
     * @date 14:55 2021/11/22
     * @param 
     * @return 
     * @other
    **/
    public Map<String,Object> failRender(){
        Map<String,Object> result = new HashMap<>(3);
        result.put(ReturnConstant.STATUS, ReturnConstant.STATUS_FAILED);
        result.put(ReturnConstant.MSG, ReturnConstant.MSG_FAILED);
        result.put(ReturnConstant.CODE, ReturnConstant.CODE_FAILED);
        return result;
    }

    /**
     * 请求失败 携带异常
     * @author song
     * @date 14:55 2021/11/22
     * @param 
     * @return 
     * @other
    **/
    public Map<String,Object> failRender(Exception exception){
        String msg = exception.getMessage();
        Map<String,Object> result = failRender();
        result.put(ReturnConstant.MSG, msg);
        return result;
    }

}
